package com.heo.music.player.handler;

import com.heo.music.player.dto.base.BadRequestMsg;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.exception.ConstraintViolationException;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * created on 2023-06-09 11:36
 *
 * @author 星天无极
 **/
@ControllerAdvice
@Slf4j
public class JpaExceptionHandler {

    @ResponseBody
    @ExceptionHandler(EmptyResultDataAccessException.class)
    public ResponseEntity<Void> handler() {
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    @ResponseBody
    @ExceptionHandler(ConstraintViolationException.class)
    public ResponseEntity<BadRequestMsg> handler(ConstraintViolationException e) {
        log.error("",e);
        BadRequestMsg badRequestMsg = new BadRequestMsg("操作失败,数据存在约束", e.getMessage());
        return ResponseEntity.status(HttpStatus.CONFLICT).body(badRequestMsg);
    }
}
